package de.tabuma.formcreator.entity;

import java.util.*;

/**
 * @author $Author: $
 * @version $Revision: $
 * @since $Date: $
 */
public class FormStructur extends FormElement {

    public FormStructur(String name,String type,int minAnzahlElemente,int maxAnzahlElemente,boolean attribute, boolean sequence,boolean choice, boolean all) {
        super(type, name, "", "",attribute);
        this.minOccursElement = minAnzahlElemente;
        this.maxOccursElement = maxAnzahlElemente;
        this.formStructur = new ArrayList<FormStructur>();
        this.sequence = sequence;
        this.choice = choice;
        this.all = all;
        this.breadcrumps = new ArrayList<FormStructur>();

    }

    FormStructur headStructur;
    public FormStructur getHeadStructur() {
        return headStructur;
    }
    public void setHeadStructur(FormStructur headStructur) {
        this.headStructur = headStructur;
    }

    ArrayList<FormStructur> breadcrumps;
    public ArrayList<FormStructur> getBreadcrumps() {
        return breadcrumps;
    }
    public void createBreadcrumps(ArrayList<FormStructur> arrayList) {
        this.breadcrumps = new ArrayList<FormStructur>();

        this.breadcrumps.addAll(arrayList);
        boolean insert = false;
        if(this.headStructur!=null) {
            if(!this.all&&!this.choice&&!this.sequence) {
                insert = true;
            }
        }
        else {
            if(this.hasComplexElements()&&this.hasSimpleElements()) {
                insert = true;
            }
        }
        if(insert) {
            this.breadcrumps.add(this);
        }
        for(int i=0;i<this.formStructur.size();i++) {
            this.formStructur.get(i).createBreadcrumps(this.breadcrumps);
        }
    }
    public void setBreadcrumps(ArrayList<FormStructur> breadcrumps) {
        this.breadcrumps = breadcrumps;
    }
    
    private boolean sequence;
    public boolean isSequence() {
        return sequence;
    }

    private boolean choice;
    public boolean isChoice() {
        return choice;
    }

    private boolean all;
    public boolean isAll() {
        return all;
    }

    public boolean isSequenceOrChoiceOrAll() {
        if(all) return true;
        if(choice) return true;
        if(sequence) return true;
        return false;
    }

    private int maxOccursElement;
    public int getMaxOccursElement() {
        return maxOccursElement;
    }

    private int minOccursElement;
    public int getMinOccursElement() {
        return minOccursElement;
    }

    private ArrayList<FormStructur> formStructur;
    public ArrayList<FormStructur> getFormStructur() {
        return formStructur;
    }

    public void setFormStructur(ArrayList<FormStructur> formStructur) {
        this.formStructur = formStructur;
    }

    public void addFormularStruktur(FormStructur formularStruktur1) {
        if(formularStruktur1!=null) {
            ArrayList<FormStructur> arrayList = this.getBreadcumpsClone();
            formularStruktur1.setHeadStructur(this);
            arrayList.add(formularStruktur1);
            formularStruktur1.setBreadcrumps(arrayList);
            this.formStructur.add(formularStruktur1);
        }
    }
    
    public void addFormularStruktur(List<FormStructur> formularStrukturList) {
        if(formularStrukturList!=null) {
            for(int i=0;i<formularStrukturList.size();i++) {
                this.addFormularStruktur(formularStrukturList.get(i));
            }
        }
    }

    public List<FormStructur> getFormularStrukturList() {
        ArrayList<FormStructur> arrayList = new ArrayList<FormStructur>();
        for(int i=0;i<this.formStructur.size();i++) {
            FormStructur fs = this.formStructur.get(i);
            if(!fs.isSimpleType()) {
                arrayList.add(fs);
                List<FormStructur> list = fs.getFormularStrukturList();
                arrayList.addAll(list);
            }
        }
        return arrayList;
    }

    public boolean hasComplexElements() {
        for(int i=0;i<this.formStructur.size();i++) {
            FormStructur fs = this.formStructur.get(i);
            if(!fs.isSimpleType()) {
                return true;
            }
        }
        return false;
    }

    public boolean hasSimpleElements() {
        for(int i=0;i<this.formStructur.size();i++) {
            FormStructur fs = this.formStructur.get(i);
            if(fs.isSimpleType()) {
                return true;
            }
        }
        return false;
    }

    public boolean hasMinAndMaxOccurs1() {
        if(this.getMinOccursElement()!=1 || this.getMaxOccursElement()!=1) {
            return false;
        }
        for(int i=0;i<this.formStructur.size();i++) {
            FormStructur fs = this.formStructur.get(i);
            if(fs.getMinOccursElement()!=1||fs.getMaxOccursElement()!=1)
                return false;
            if(!fs.hasMinAndMaxOccurs1()) {
                return false;
            }
        }
        return true;
    }

    public ArrayList<FormStructur> getBreadcumpsClone() {
        ArrayList<FormStructur> fsList = new ArrayList<FormStructur>();
        for(int i=0;i<this.breadcrumps.size();i++) {
            fsList.add(breadcrumps.get(i));
        }
        return fsList;
    }
}
